package com.fjwt.gz.proxy.ctrl.blackList;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fjwt.gz.components.oss.model.OssFileConfig;
import com.fjwt.gz.core.model.ApiRes;
import com.fjwt.gz.db.dto.BlacklistDTO;
import com.fjwt.gz.db.vo.BlacklistVO;
import com.fjwt.gz.proxy.ctrl.CommonCtrl;
import com.fjwt.gz.service.impl.BlacklistService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 黑名单明细表 前端控制器
 * </p>
 *
 * @author wangjie
 * @since 2025-05-12
 */
@Slf4j
@RestController
@RequestMapping("/api/blacklist")
public class BlacklistController extends CommonCtrl {

    @Autowired
    private BlacklistService blacklistService;


    /**
     * 获取黑名单列表
     *
     * @return
     */
//    @PreAuthorize("hasAnyAuthority('ENT_BLACKLIST_INFO')")
    @GetMapping("/list")
    public ApiRes getBlacklistList() {
        BlacklistDTO blacklistDTO = getObject(BlacklistDTO.class);
        log.info("机构端获取黑名单列表接口，请求地址：api/blacklist/list, 请求参数：{}", JSONUtil.toJsonStr(blacklistDTO));
        IPage<BlacklistVO> page = blacklistService.getBlacklistList(getIPage(true), blacklistDTO);
        log.info("机构端获取黑名单列表接口，请求地址：api/blacklist/list, 返回结果：{}", JSONUtil.toJsonStr(page));
        return ApiRes.page(page);
    }

    /**
     * 导入黑名单列表
     *
     * @return
     */
//    @PreAuthorize("hasAnyAuthority('ENT_BLACKLIST_IMPORT')")
    @PostMapping("/import")
    public ApiRes importBlacklistList() {
        BlacklistDTO blacklistDTO = getObject(BlacklistDTO.class);
        log.info("机构端导入黑名单接口，请求地址：api/blacklist/import, 请求参数：{}", JSONUtil.toJsonStr(blacklistDTO));
        blacklistService.importBlacklistList(blacklistDTO, OssFileConfig.BIZ_TYPE.UPLOAD);
        return ApiRes.ok();
    }

    /**
     * 导出黑名单列表
     *
     * @return
     */
//    @PreAuthorize("hasAnyAuthority('ENT_BLACKLIST_EXPORT')")
    @PostMapping("/export")
    public void exportBlacklistList() {
        BlacklistDTO blacklistDTO = getObject(BlacklistDTO.class);
        log.info("机构端导出黑名单接口，请求地址：api/blacklist/export, 请求参数：{}", JSONUtil.toJsonStr(blacklistDTO));
        blacklistService.exportBlacklistByExcel(blacklistDTO, response);
    }

    /**
     * 删除黑名单列表
     *
     * @return
     */
//    @PreAuthorize("hasAnyAuthority('ENT_BLACKLIST_DELETE')")
    @PostMapping("/delete")
    public ApiRes deleteBlacklistList() {
        Long blacklistId = getValLong("blacklistId");
        log.info("机构端删除黑名单接口，请求地址：api/blacklist/delete, 请求参数：{}", JSONUtil.toJsonStr(blacklistId));
        if (blacklistId == null) {
            return ApiRes.customFail("参数错误");
        }
        blacklistService.removeByListId(blacklistId);
        return ApiRes.ok();
    }

}

